package com.sum.tea.rongrui

import android.os.Bundle
import android.view.KeyEvent
import androidx.lifecycle.lifecycleScope
import com.example.lib_rongrui.RrReader
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.databinding.ActivityFindingBinding
import com.sum.tea.rongrui.viewmodel.FindViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FindingActivity :  BaseMvvmActivity<ActivityFindingBinding, FindViewModel>() {

    private var mWorking = true
    private var readJob: Job? = null
    private var rssi = 0
    var keyPress = false

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.btfind.setOnClickListener {
            readTag()
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.epcId.text = ScanModeActivity.epc
        mBinding.circleProgress.value = 0.00f
    }

    private fun readTag() {
        lifecycleScope.launch {
            val findingText = getString(R.string.finding)
            if (mBinding.btfind.text.toString() == findingText) {
                if (readJob == null) {
                    mWorking = true
                    mBinding.btfind.setText(R.string.btstop)
                    readJob = lifecycleScope.launch(Dispatchers.IO) {
                        while (mWorking) {
                            val mtag = RrReader.rrlib.FindEPC(ScanModeActivity.epc)
                            if (mtag != null) {
                                rssi = mtag.rssi
                                RrReader.rrlib.playSound()
                                withContext(Dispatchers.Main) {
                                    mBinding.circleProgress.value = rssi.toFloat()
                                }
                            } else {
                                if (rssi > 0) rssi -= 2
                                if (rssi < 0) rssi = 0
                                withContext(Dispatchers.Main) {
                                    mBinding.circleProgress.value = rssi.toFloat()
                                }
                            }
                            delay(500) // 根据需要调整延迟时间
                        }
                    }
                }
            } else {
                if (readJob != null) {
                    mWorking = false
                    readJob?.join()
                    readJob = null
                    mBinding.btfind.setText(R.string.finding)
                }
            }
        }
    }

    /**
     * 按键扫描RFID
     **/
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 523 && !keyPress) {
            keyPress = true
            readTag()
            return true
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    /**
     * 按键扫描RFID
     **/
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 523) {
            keyPress = false
            return true
        }
        return super.onKeyUp(keyCode, event)
    }
}
